'''
Created on Sep 7, 2012

@author: petrina
'''
from link_indicator import Link_indicator
from indiutils import Indiutils
import logging

class Planning_time_index(Link_indicator):
    '''
    Calculates the Planning-Time-Index for every time interval (using aggregated fcd-data).
    
    Formula for calculating the indicator: 
        pti_i = wtt_i/ftt
            where:
                - pti_i ... planning time index at time interval i
                - wtt_i...  worst case travel time at time interval i
                - ftt ... free flow travel time
    
    The 95th percentile travel time acts as worst case travel time (@see processing.planning_time)
    '''


    def __init__(self, link):
        '''        
        @param link: Link-object (containing valid records and timeseries) 
        '''
        
        Link_indicator.__init__(self, link)
        self._name = 'Planning Time Index'
        self._mr_name =  'plan_time_idx'
        self._unit = 'index'
        #self.unit.__doc__ = 'percent (%)'
        
        self._create_title(link.id, link.direction)
        
        logging.debug("Planning_time_index: created new Planning_time_index")
        
    #----------------------public methods of the class-------------------------

    def calculate(self):
        '''
        @return: planning time index for every time interval as list of Result_values
        '''
        
        logging.info("Planning_time_index: staring to calculate the planning time index")
        
        utils = Indiutils()
        pt = utils.p95_tt(self._link.timeseries, self._link.length) 
        
        self._values = []
        for i in range(len(pt)):
            # no propability value yet
            pti = None if pt[i] is None or self._link.freett is None or self._link.freett == 0 else pt[i]/self._link.freett
            self._values.append(pti)
        
        logging.debug("Planning_time_index: finished calculating the planning time index")

        return self._values